package br.com.prime.model.bussines;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.prime.model.ConnectionFactory;
import br.com.prime.model.pojo.Nota;

public class NotaDAO {

	private Connection connection;

	public NotaDAO() {
		try {
			this.connection = new ConnectionFactory().getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Nota> getAll() {
		List<Nota> notas = new ArrayList<Nota>();
		String sql = "SELECT * FROM nota";
		try {
			PreparedStatement stmt = this.connection.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			notas = montarObjeto(rs);
			rs.close();
			stmt.close();
			return notas;
		} catch (SQLException e) {
			e.printStackTrace();
			return notas;
		}
	}

	public List<Nota> montarObjeto(ResultSet rs) {
		List<Nota> notas = new ArrayList<Nota>();
		try {
			while (rs.next()) {
				Nota nota = new Nota();
				nota.setID_NOTA(rs.getInt("iD_NOTA"));
				nota.setID_MATERIA(rs.getInt("iD_MATERIA"));
				nota.setID_ALUNO(rs.getInt("iD_ALUNO"));
				nota.setVALOR(rs.getInt("VALOR"));
					
				notas.add(nota);
			}
			return notas;
		} catch (Exception e) {
			e.printStackTrace();
			return notas;
		}
	}
}